Loading Data
setwd('/Users/Frankie/Documents/School/Stat 133/Labs')
load('nba2018-salary-points.RData')
Subsetting Vectors
four <- head(player, n = 4)
four[1]
## [1] "Al Horford"
four[0]
## character(0)
four[-1]
## [1] "Amir Johnson" "Avery Bradley" "Demetrius Jackson"
four[-c(1, 2, 3)]
## [1] "Demetrius Jackson"
four[5]
## [1] NA
four[c(1, 2, 2, 3, 3, 3)]
## [1] "Al Horford" "Amir Johnson" "Amir Johnson" "Avery Bradley"
## [5] "Avery Bradley" "Avery Bradley"
#Your Turn
##Even players
player[seq(1, length(player), 2)]
## [1] "Al Horford" "Avery Bradley"
## [3] "Gerald Green" "Jae Crowder"
## [5] "Jaylen Brown" "Jordan Mickey"
## [7] "Marcus Smart" "Tyler Zeller"
## [9] "Channing Frye" "Deron Williams"
## [11] "Edy Tavares" "J.R. Smith"
## [13] "Jordan McRae" "Kevin Love"
## [15] "Kyrie Irving" "LeBron James"
## [17] "Tristan Thompson" "Cory Joseph"
## [19] "DeMar DeRozan" "Fred VanVleet"
## [21] "Jonas Valanciunas" "Lucas Nogueira"
## [23] "P.J. Tucker" "Patrick Patterson"
## [25] "Bojan Bogdanovic" "Brandon Jennings"
## [27] "Daniel Ochefu" "Ian Mahinmi"
## [29] "John Wall" "Marcin Gortat"
## [31] "Otto Porter" "Trey Burke"
## [33] "Dennis Schroder" "Ersan Ilyasova"
## [35] "Jose Calderon" "Kris Humphries"
## [37] "Malcolm Delaney" "Mike Muscala"
## [39] "Ryan Kelly" "Thabo Sefolosha"
## [41] "Axel Toupane" "Giannis Antetokounmpo"
## [43] "Jabari Parker" "John Henson"
## [45] "Malcolm Brogdon" "Michael Beasley"
## [47] "Mirza Teletovic" "Spencer Hawes"
## [49] "Terrence Jones" "Tony Snell"
## [51] "Al Jefferson" "Georges Niang"
## [53] "Jeff Teague" "Kevin Seraphin"
## [55] "Lavoy Allen" "Myles Turner"
## [57] "Rakeem Christmas" "Thaddeus Young"
## [59] "Bobby Portis" "Cristiano Felicio"
## [61] "Dwyane Wade" "Jerian Grant"
## [63] "Joffrey Lauvergne" "Nikola Mirotic"
## [65] "R.J. Hunter" "Robin Lopez"
## [67] "Goran Dragic" "James Johnson"
## [69] "Josh Richardson" "Luke Babbitt"
## [71] "Rodney McGruder" "Udonis Haslem"
## [73] "Willie Reed" "Aron Baynes"
## [75] "Boban Marjanovic" "Henry Ellenson"
## [77] "Jon Leuer" "Marcus Morris"
## [79] "Reggie Bullock" "Stanley Johnson"
## [81] "Aaron Harrison" "Briante Weber"
## [83] "Cody Zeller" "Jeremy Lamb"
## [85] "Kemba Walker" "Marvin Williams"
## [87] "Mike Tobey" "Ramon Sessions"
## [89] "Carmelo Anthony" "Courtney Lee"
## [91] "Joakim Noah" "Kristaps Porzingis"
## [93] "Lance Thomas" "Maurice Ndour"
## [95] "Ron Baker" "Willy Hernangomez"
## [97] "Anthony Brown" "Bismack Biyombo"
## [99] "C.J. Wilcox" "Damjan Rudez"
## [101] "Evan Fournier" "Jodie Meeks"
## [103] "Mario Hezonja" "Patricio Garino"
## [105] "Terrence Ross" "Dario Saric"
## [107] "Hollis Thompson" "Jerryd Bayless"
## [109] "Justin Anderson" "Nik Stauskas"
## [111] "Robert Covington" "Shawn Long"
## [113] "Tiago Splitter" "Andrew Nicholson"
## [115] "Archie Goodwin" "Caris LeVert"
## [117] "Isaiah Whitehead" "Joe Harris"
## [119] "K.J. McDaniels" "Quincy Acy"
## [121] "Rondae Hollis-Jefferson" "Spencer Dinwiddie"
## [123] "Yogi Ferrell" "Andre Iguodala"
## [125] "David West" "Ian Clark"
## [127] "JaVale McGee" "Kevon Looney"
## [129] "Matt Barnes" "Shaun Livingston"
## [131] "Zaza Pachulia" "Danny Green"
## [133] "Davis Bertans" "Dewayne Dedmon"
## [135] "Jonathon Simmons" "Kyle Anderson"
## [137] "Manu Ginobili" "Patty Mills"
## [139] "Tony Parker" "Chinanu Onuaku"
## [141] "Eric Gordon" "James Harden"
## [143] "Montrezl Harrell" "Patrick Beverley"
## [145] "Sam Dekker" "Troy Williams"
## [147] "Austin Rivers" "Brandon Bass"
## [149] "Chris Paul" "Diamond Stone"
## [151] "Jamal Crawford" "Marreese Speights"
## [153] "Raymond Felton" "Alec Burks"
## [155] "Dante Exum" "George Hill"
## [157] "Jeff Withey" "Joe Johnson"
## [159] "Raul Neto" "Rudy Gobert"
## [161] "Trey Lyles" "Andre Roberson"
## [163] "Doug McDermott" "Jerami Grant"
## [165] "Kyle Singler" "Norris Cole"
## [167] "Semaj Christon" "Taj Gibson"
## [169] "Andrew Harrison" "Chandler Parsons"
## [171] "James Ennis" "Jarell Martin"
## [173] "Mike Conley" "Tony Allen"
## [175] "Vince Carter" "Wayne Selden"
## [177] "Al-Farouq Aminu" "CJ McCollum"
## [179] "Ed Davis" "Jake Layman"
## [181] "Maurice Harkless" "Noah Vonleh"
## [183] "Shabazz Napier" "Danilo Gallinari"
## [185] "Emmanuel Mudiay" "Jamal Murray"
## [187] "Jarnell Stokes" "Kenneth Faried"
## [189] "Mason Plumlee" "Nikola Jokic"
## [191] "Will Barton" "Alexis Ajinca"
## [193] "Cheick Diallo" "DeMarcus Cousins"
## [195] "E'Twaun Moore" "Jordan Crawford"
## [197] "Omer Asik" "Quinn Cook"
## [199] "Solomon Hill" "A.J. Hammons"
## [201] "DeAndre Liggins" "Dirk Nowitzki"
## [203] "Dwight Powell" "J.J. Barea"
## [205] "Jonathan Gibson" "Nerlens Noel"
## [207] "Pierre Jackson" "Seth Curry"
## [209] "Anthony Tolliver" "Ben McLemore"
## [211] "Darren Collison" "Georgios Papagiannis"
## [213] "Kosta Koufos" "Malachi Richardson"
## [215] "Skal Labissiere" "Tyreke Evans"
## [217] "Adreian Payne" "Brandon Rush"
## [219] "Gorgui Dieng" "Jordan Hill"
## [221] "Kris Dunn" "Ricky Rubio"
## [223] "Tyus Jones" "Brandon Ingram"
## [225] "D'Angelo Russell" "Ivica Zubac"
## [227] "Julius Randle" "Metta World Peace"
## [229] "Tarik Black" "Timofey Mozgov"
## [231] "Alan Williams" "Brandon Knight"
## [233] "Devin Booker" "Elijah Millsap"
## [235] "Jared Dudley" "John Jenkins"
## [237] "Marquese Chriss" "T.J. Warren"
## [239] "Tyson Chandler"
##Odd salary
salary[seq(2, length(salary), 2)]
## [1] 12000000 1450000 6587132 1825200 5000000 3094014 1906440
## [8] 242224 24022 402043 9700000 1551659 543471 5239437
## [15] 207722 2500000 1589640 1577280 14200000 2703960 12000000
## [22] 874636 1196040 12250000 22116750 1191480 543471 5000000
## [29] 2006640 7400000 2870813 1499760 23180275 72193 15730338
## [36] 128623 4837500 20072033 2318280 2281605 35166 17100000
## [43] 1551659 15200000 9607500 12500000 1811040 1551659 2568600
## [50] 2700000 4583450 1050500 1052342 4000000 10770000 18314532
## [57] 7000000 3488000 2112480 2092200 1015696 17552209 3183526
## [64] 750000 14000000 2898000 22116750 5782450 2593440 374933
## [71] 5628000 6000000 22116750 1551659 874636 6000000 3678319
## [78] 650000 14956522 17200000 1050961 874636 2730000 276827
## [85] 6333333 13000000 20869566 543471 243860 21323250 1015696
## [92] 3900000 543471 2898000 1410598 4351320 440078 5000000
## [99] 7250000 2613600 15000000 31969 11750000 950000 38903
## [106] 9000000 4788840 4826160 57672 1025831 8000000 874636
## [113] 1326960 1015696 21165675 4347826 11483254 3000000 5000000
## [120] 2500000 980431 9250000 1551659 1171560 15330435 980431
## [127] 26540100 16663575 543471 12112359 543471 1551659 1180080
## [134] 663810 17638063 20575005 210995 15500000 934293 1296240
## [141] 305000 7000000 2898000 18735364 7806971 1315448 20140838
## [148] 1273920 21165675 7377500 2203000 3500000 5628000 7000000
## [155] 11050000 16073140 2250000 600000 1406520 2433334 5994764
## [162] 2440200 17145838 1191480 3750000 26540100 3140517 6552960
## [169] 5700000 1369229 980431 21165675 603558 3332940 1793760
## [176] 10361445 18500000 24328425 16393443 1921320 9213484 874636
## [183] 495227 8070175 1655880 4540525 1987440 1627320 3500000
## [190] 5000000 11200000 22116750 2978250 576724 91274 11286518
## [197] 3000000 252676 2090000 31969 4228000 543471 22116750
## [204] 63938 119494 543471 874636 17100000 12500000 3517200
## [211] 8000000 82979 5200000 13333333 1315448 3551160 6006600
## [218] 7643979 635753 5960160 3800000 3046299 2240880 7600000
## [225] 137466 12500000 18000000 5443918 1050961 1733880 4823621
## [232] 543471 4276320 14000000 102898 4000000 465143 918369
#all multiples of 5 (e.g. 5, 10, 15, etc) of team
team[seq(5, length(salary), 5)]
## [1] BOS BOS BOS CLE CLE CLE TOR TOR TOR WAS WAS WAS ATL ATL ATL ATL MIL
## [18] MIL MIL IND IND IND IND CHI CHI CHI MIA MIA MIA DET DET DET CHO CHO
## [35] CHO NYK NYK NYK ORL ORL ORL PHI PHI PHI PHI BRK BRK BRK BRK GSW GSW
## [52] GSW SAS SAS SAS HOU HOU HOU LAC LAC LAC UTA UTA UTA OKC OKC OKC MEM
## [69] MEM MEM POR POR POR DEN DEN DEN NOP NOP NOP DAL DAL DAL DAL SAC SAC
## [86] SAC MIN MIN MIN LAL LAL LAL PHO PHO PHO
## 30 Levels: ATL BOS BRK CHI CHO CLE DAL DEN DET GSW HOU IND LAC LAL ... WAS
#elements in positions 10, 20, 30, 40, etc of scored
position[seq(10, length(position), 10)]
## [1] "PF" "PF" "C" "C" "SG" "PF" "SF" "SG" "PG" "PG" "SG" "SG" "PG" "PF"
## [15] "SG" "PF" "SG" "PG" "SG" "PG" "PF" "C" "C" "SG" "PF" "PG" "SF" "C"
## [29] "SF" "SG" "PF" "PG" "PF" "C" "PG" "C" "SG" "C" "PG" "PF" "SG" "SG"
## [43] "PG" "C" "SG" "PG" "SF"
#all the even elements in team but this time in reverse order
team[seq(length(salary) - 1, 2,-2)]
## [1] PHO PHO PHO PHO PHO PHO PHO PHO LAL LAL LAL LAL LAL LAL LAL MIN MIN
## [18] MIN MIN MIN MIN MIN SAC SAC SAC SAC SAC SAC SAC SAC DAL DAL DAL DAL
## [35] DAL DAL DAL DAL DAL NOP NOP NOP NOP NOP NOP NOP NOP DEN DEN DEN DEN
## [52] DEN DEN DEN DEN DEN POR POR POR POR POR POR MEM MEM MEM MEM MEM MEM
## [69] MEM MEM OKC OKC OKC OKC OKC OKC OKC OKC UTA UTA UTA UTA UTA UTA UTA
## [86] LAC LAC LAC LAC LAC LAC LAC LAC HOU HOU HOU HOU HOU HOU HOU SAS SAS
## [103] SAS SAS SAS SAS SAS SAS GSW GSW GSW GSW GSW GSW GSW GSW BRK BRK BRK
## [120] BRK BRK BRK BRK BRK BRK PHI PHI PHI PHI PHI PHI PHI PHI PHI ORL ORL
## [137] ORL ORL ORL ORL ORL ORL ORL NYK NYK NYK NYK NYK NYK NYK CHO CHO CHO
## [154] CHO CHO CHO CHO CHO DET DET DET DET DET DET DET DET MIA MIA MIA MIA
## [171] MIA MIA MIA CHI CHI CHI CHI CHI CHI CHI CHI IND IND IND IND IND IND
## [188] IND IND MIL MIL MIL MIL MIL MIL MIL MIL MIL ATL ATL ATL ATL ATL ATL
## [205] ATL ATL ATL WAS WAS WAS WAS WAS WAS WAS TOR TOR TOR TOR TOR TOR TOR
## [222] TOR CLE CLE CLE CLE CLE CLE CLE CLE CLE BOS BOS BOS BOS BOS BOS BOS
## 30 Levels: ATL BOS BRK CHI CHO CLE DAL DEN DET GSW HOU IND LAC LAL ... WAS
Logical Subsetting and Comparisons
#Your Turn
##players in position Center, of Warriors (GSW)
player[team == "GSW" & position == "C"]
## [1] "Anderson Varejao" "Damian Jones" "David West"
## [4] "JaVale McGee" "Kevon Looney" "Zaza Pachulia"
##players of both GSW (warriors) and LAL (lakers)
player[team == "GSW" | team == "LAL"]
## [1] "Anderson Varejao" "Andre Iguodala" "Damian Jones"
## [4] "David West" "Draymond Green" "Ian Clark"
## [7] "James Michael McAdoo" "JaVale McGee" "Kevin Durant"
## [10] "Kevon Looney" "Klay Thompson" "Matt Barnes"
## [13] "Patrick McCaw" "Shaun Livingston" "Stephen Curry"
## [16] "Zaza Pachulia" "Brandon Ingram" "Corey Brewer"
## [19] "D'Angelo Russell" "David Nwaba" "Ivica Zubac"
## [22] "Jordan Clarkson" "Julius Randle" "Luol Deng"
## [25] "Metta World Peace" "Nick Young" "Tarik Black"
## [28] "Thomas Robinson" "Timofey Mozgov" "Tyler Ennis"
#players in positions Shooting Guard and Point Guards, of Lakers (LAL)
player[(position == "PG" | position == "SG") & team == "LAL"]
## [1] "D'Angelo Russell" "David Nwaba" "Jordan Clarkson"
## [4] "Nick Young" "Tyler Ennis"
#subset Small Forwards of GSW and LAL
player[position == "SF" & (team == "LAL" | team == "GSW")]
## [1] "Andre Iguodala" "Matt Barnes" "Brandon Ingram"
## [4] "Corey Brewer" "Luol Deng" "Metta World Peace"
#name of the player with largest salary
player[salary == max(salary)]
## [1] "LeBron James"
#name of the player with smallest salary
player[salary == min(salary)]
## [1] "Edy Tavares"
#name of the player with largest number of scored points
player[scored == max(scored)]
## [1] "Russell Westbrook"
#salary of the player with largest number of points
salary[scored == max(scored)]
## [1] 26540100
#largest salary of all Centers
player[position == 'C'][which.max(salary[position == 'C'])]
## [1] "Al Horford"
#team of the player with the largest number of scored points
team[scored == max(scored)]
## [1] OKC
## 30 Levels: ATL BOS BRK CHI CHO CLE DAL DEN DET GSW HOU IND LAC LAL ... WAS
#name of the player with the largest number of 3-pointers
player[points3 == max(points3)]
## [1] "Stephen Curry"
Some plotting
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
plot_ly(x = scored, y = salary, type = "scatter", mode = "markers")
log_scored <- log(scored)
log_salary <- log(salary)
plot(log_scored, log_salary)
text(log_scored, log_salary, labels = abbreviate(player))

#Your Turn
par(mfrow = c(1,2))
GSW_score <- scored[team == "GSW"]
GSW_player <- player[team == "GSW"]
GSW_salary <- salary[team == "GSW"]
plot(GSW_score, GSW_salary)
text(GSW_score, GSW_salary, labels = abbreviate(GSW_player))
plot(log(GSW_score), log(GSW_salary))
text(log(GSW_score), log(GSW_salary), labels = abbreviate(GSW_player))

Factors
position_fac <- factor(position)
table(position_fac)
## position_fac
## C PF PG SF SG
## 97 98 96 84 102
position_fac[team == "GSW"]
## [1] C SF C C PF SG PF C PF C SG SF SG PG PG C
## Levels: C PF PG SF SG
position_fac[salary > 15000000]
## [1] C PF PG SF C SG SG C PG C SF PF C SF SF SG SF PG C C PF SG SF
## [24] PG C C SG C PF PF SG SF PF C PG PF PF PG C SF C PG SF C PG SG
## [47] PG SF SF C C PF PF SG SF C
## Levels: C PF PG SF SG
table(position[salary > 15000000])
##
## C PF PG SF SG
## 17 10 9 12 8
table(team[position_fac == "SG"])
##
## ATL BOS BRK CHI CHO CLE DAL DEN DET GSW HOU IND LAC LAL MEM MIA MIL MIN
## 3 3 4 5 5 4 4 4 3 3 3 2 3 3 3 4 4 2
## NOP NYK OKC ORL PHI PHO POR SAC SAS TOR UTA WAS
## 2 4 2 5 3 5 3 5 4 2 2 3
#Your Turn
plot(scored, salary, col = position_fac, pch = c(1, 2, 3, 4), cex = c(.2,.2,.2,.2), xlab = "Points Scored", ylab = "Salary in Dollars")
